package com.zhf.server.impl;

import com.zhf.model.*;
import com.zhf.server.Api;
import com.zhf.server.ExamService;
import com.zhf.server.ServerDao;
import com.zhf.server.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api("/exam")
public class ExamServiceImpl implements ExamService {
    ServerDao serverDao;
    public ExamServiceImpl(ServerDao serverDao){
        this.serverDao = serverDao;
    }


    @Override
    @Api("/startExamination")
    public ExamUserMessage startExamination(ExamUser examUser) {
        ExamUserMessage examUserMessage = new ExamUserMessage();
        // 需要检查文件系统中考题数量和该用户的考题名称是否已经存在
        List<Question> questions = serverDao.listQuestion();
        System.out.println("当前系统中所有的问题有：" + questions);
        if(questions == null || questions.size() < 5){
            examUserMessage.setExamUser(examUser);
            examUserMessage.setMessage("notEnoughFail");
            return examUserMessage;
        }
        else{
            // 题量足够的话，还需要校验当前当前用户对应的考题名称是否已经存在了
            ExamUserMessage examUserMessage1 = selectExamUserByExamNameAndUser(examUser);
            if("success".equals(examUserMessage1.getMessage())){
                // 已经存在，则直接返回已经存在
                examUserMessage.setExamUser(examUser);
                examUserMessage.setMessage("examNameExit");
                return examUserMessage;
            }else{
                // 如果不存在则生成本次考试的五个考题返回给客户端
                ArrayList<Integer> integers = CommonUtil.generateFiveNum(questions.size());
                // 生成五个Question对象
                List<Question> questionsGeneratedList = new ArrayList<>();
                for (int index: integers) {
                    questionsGeneratedList.add(questions.get(index));
                }
                questionsGeneratedList.stream().map(t -> {
                    Question que = new Question();
                    que.setQuestionName(t.getQuestionName());
                    que.setOptionA(t.getAnswer());
                    que.setOptionB(t.getOptionB());
                    que.setOptionC(t.getOptionC());
                    que.setOptionD(t.getOptionD());
                    return que;
                }).collect(Collectors.toList());
                examUser.setQuestions(questionsGeneratedList);
                examUserMessage.setExamUser(examUser);
                examUserMessage.setMessage("success");
            }
        }
        return examUserMessage;
    }

    @Override
    @Api("/staticExamResult")
    public ExamUserMessage staticExamResult(ExamUser examUser) {
        ExamUserMessage examUserMessage = new ExamUserMessage();
        List<Question> questions = examUser.getQuestions();
        List<Question> questionsList = serverDao.listQuestion();
        int score = 0;
        for (Question e : questions) {
            List<Question> collect = questionsList.stream().filter(t -> e.getQuestionName().equals(t.getQuestionName())).collect(Collectors.toList());
            if (collect != null && collect.size() == 1) {
                Question q = collect.get(0);
                System.out.println("进来统计了吗？");
                if (e.getAnswer().equals(q.getAnswer())) {
                    score = score + 20;
                }
            }
        }
        examUser.setGrade(score);
        // 将该次考试结果存入数据库中
        boolean isSaved = serverDao.saveExamUser(examUser);
        examUserMessage.setExamUser(examUser);
        if(isSaved){
            examUserMessage.setMessage("success");
        }
        else{
            examUserMessage.setMessage("fail");
        }
        return examUserMessage;
    }

    @Override
    @Api("/selectExamUserByExamNameAndUser")
    public ExamUserMessage selectExamUserByExamNameAndUser(ExamUser examUser) {
        ExamUserMessage examUserMessage = serverDao.selectExamUserByExamNameAndUser(examUser);
        return examUserMessage;
    }

    @Override
    @Api("/exportExamGrade")
    public ExamUserListMessage selectExamUserListByUser(User user) {
        ExamUserListMessage examUserListMessage = serverDao.selectExamUserListByUser(user);
        return examUserListMessage;
    }
}
